<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Models\Suggest;
class SuggestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 
    }

    /**
     * 显示用户需要提交反馈的表单
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home.suggest.create');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 获取用户提交数据
        $request = $request->except('_token');

        // 将用户提交的数据放到数据库
        $sug = new Suggest;
        $sug -> s_pic = $request['s_pic'];
        $sug -> s_title = $request['s_title'];
        $sug -> s_content = $request['s_content'];
        $data = $sug-> save();

        // 判断提交数据是否成功
        if($data)
        {    // 用户反馈提交成功   
            echo "<script>alert('提交成功');location.href='/'</script>";
            
        }else{

            // 用户反馈提交失败
            echo "<script>alert('提交失败');location.href='/home/suggest/create'</script>";
        };

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

   public function upload(Request $request)
    {
            //获取文件对象
        $file = Input::file('pic');

        // 1.检测是否有文件上传
        // if($request -> hasFile($data['web_logo'])){
        if($file->isValid()){
            // 处理文件名称
            $temp_name = md5(time()+rand(1000,9999));
            
            // 获取后缀
            $hz = $request -> file('pic') ->getClientOriginalExtension();
          
            // 文件路径
            $temp_path = './uploads/'.date('Ymd',time()); 
              // 拼接文件名
            $name = ltrim($temp_path.'/'.$temp_name.'.'.$hz,'.');
            $temp_name2 =  $temp_name.'.'.$hz;
            // echo $temp_name2.'<br>';
            // echo $name;
            // 文件上传
            $request -> file('pic') -> move($temp_path,$temp_name2);

            return  $name;
        }
    }
}
